import { DataSource } from 'typeorm';
import { Permission, PermissionGroup } from '../../../entities/permission.entity';

/**
 * 权限信息接口
 */
interface PermissionInfo {
  code: string;
  name: string;
  description: string;
  group?: PermissionGroup;
}

/**
 * 权限种子数据
 * 负责初始化系统基础权限
 */
export class PermissionSeed {
  constructor(private dataSource: DataSource) {}

  /**
   * 运行权限种子
   */
  async run(): Promise<void> {
    console.log('开始初始化权限数据...');

    const permissionRepository = this.dataSource.getRepository(Permission);

    // 系统基础权限
    const systemPermissions: PermissionInfo[] = [
      { 
        name: '用户管理', 
        code: 'user:manage', 
        description: '管理用户账户的权限',
        group: PermissionGroup.SYSTEM
      },
      { 
        name: '角色管理', 
        code: 'role:manage', 
        description: '管理角色的权限',
        group: PermissionGroup.SYSTEM
      },
      { 
        name: '权限管理', 
        code: 'permission:manage', 
        description: '管理权限的权限',
        group: PermissionGroup.SYSTEM
      },
    ];

    // 工作区权限
    const workspacePermissions: PermissionInfo[] = [
      { 
        name: '工作区管理', 
        code: 'workspace:manage', 
        description: '管理工作区的权限',
        group: PermissionGroup.WORKSPACE
      },
    ];

    // 项目权限
    const projectPermissions: PermissionInfo[] = [
      { 
        name: '项目管理', 
        code: 'project:manage', 
        description: '管理项目的权限',
        group: PermissionGroup.PROJECT
      },
    ];

    // 任务权限
    const taskPermissions: PermissionInfo[] = [
      { 
        name: '任务管理', 
        code: 'task:manage', 
        description: '管理任务的权限',
        group: PermissionGroup.TASK
      },
    ];

    // 标签权限
    const tagPermissions: PermissionInfo[] = [
      { 
        name: '标签管理', 
        code: 'tag:manage', 
        description: '管理标签的权限',
        group: PermissionGroup.SYSTEM
      },
    ];

    // 角色权限
    const rolePermissions: PermissionInfo[] = [
      {
        name: '角色分配',
        code: 'role:assign',
        description: '分配角色给用户的权限',
        group: PermissionGroup.SYSTEM
      },
    ];

    // 创建基础权限
    for (const permissionData of systemPermissions) {
      const existingPermission = await permissionRepository.findOneBy({
        code: permissionData.code,
      });

      if (!existingPermission) {
        const permission = permissionRepository.create(permissionData);
        await permissionRepository.save(permission);
        console.log(`创建权限: ${permission.name} (${permission.code})`);
      } else {
        console.log(`权限已存在: ${existingPermission.name} (${existingPermission.code})`);
      }
    }

    // 详细权限列表
    const detailedPermissions = [
      // 工作区权限
      {
        name: '创建工作区',
        code: 'workspace:create',
        description: '创建新的工作区',
        group: PermissionGroup.WORKSPACE,
      },
      {
        name: '查看工作区',
        code: 'workspace:view',
        description: '查看工作区信息',
        group: PermissionGroup.WORKSPACE,
      },
      {
        name: '编辑工作区',
        code: 'workspace:edit',
        description: '编辑工作区信息',
        group: PermissionGroup.WORKSPACE,
      },
      {
        name: '删除工作区',
        code: 'workspace:delete',
        description: '删除工作区',
        group: PermissionGroup.WORKSPACE,
      },
      {
        name: '添加工作区成员',
        code: 'workspace:member:add',
        description: '向工作区添加新成员',
        group: PermissionGroup.WORKSPACE,
      },
      {
        name: '查看工作区成员',
        code: 'workspace:member:view',
        description: '查看工作区成员列表',
        group: PermissionGroup.WORKSPACE,
      },
      {
        name: '编辑工作区成员',
        code: 'workspace:member:edit',
        description: '编辑工作区成员信息和权限',
        group: PermissionGroup.WORKSPACE,
      },
      {
        name: '移除工作区成员',
        code: 'workspace:member:remove',
        description: '从工作区移除成员',
        group: PermissionGroup.WORKSPACE,
      },
      
      // 任务评论权限
      { 
        name: '创建任务评论', 
        code: 'task:comment:create', 
        description: '创建任务的评论',
        group: PermissionGroup.TASK
      },
      { 
        name: '查看任务评论', 
        code: 'task:comment:read', 
        description: '查看任务的评论',
        group: PermissionGroup.TASK
      },
      { 
        name: '更新任务评论', 
        code: 'task:comment:update', 
        description: '更新任务的评论',
        group: PermissionGroup.TASK
      },
      { 
        name: '删除任务评论', 
        code: 'task:comment:delete', 
        description: '删除任务的评论',
        group: PermissionGroup.TASK
      },
      
      // 评论回复权限
      { 
        name: '创建评论回复', 
        code: 'task:comment:reply:create', 
        description: '创建评论的回复',
        group: PermissionGroup.TASK
      },
      { 
        name: '查看评论回复', 
        code: 'task:comment:reply:read', 
        description: '查看评论的回复',
        group: PermissionGroup.TASK
      },
      { 
        name: '更新评论回复', 
        code: 'task:comment:reply:update', 
        description: '更新评论的回复',
        group: PermissionGroup.TASK
      },
      { 
        name: '删除评论回复', 
        code: 'task:comment:reply:delete', 
        description: '删除评论的回复',
        group: PermissionGroup.TASK
      },
    ];

    // 创建详细权限
    for (const permissionData of detailedPermissions) {
      const existingPermission = await permissionRepository.findOneBy({
        code: permissionData.code,
      });

      if (!existingPermission) {
        const permission = permissionRepository.create(permissionData);
        await permissionRepository.save(permission);
        console.log(`创建权限: ${permission.name} (${permission.code})`);
      } else {
        console.log(`权限已存在: ${existingPermission.name} (${existingPermission.code})`);
      }
    }

    // 添加通知相关权限
    const notificationPermissions: PermissionInfo[] = [
      {
        code: 'notification:read',
        name: '查看通知',
        description: '允许查看通知列表和通知详情',
        group: PermissionGroup.USER,
      },
      {
        code: 'notification:update',
        name: '更新通知',
        description: '允许标记通知为已读',
        group: PermissionGroup.USER,
      },
      {
        code: 'notification:delete',
        name: '删除通知',
        description: '允许删除通知',
        group: PermissionGroup.USER,
      }
    ];

    // 添加文件管理相关权限
    const filePermissions: PermissionInfo[] = [
      {
        code: 'files:create',
        name: '上传文件',
        description: '允许上传文件',
        group: PermissionGroup.TASK,
      },
      {
        code: 'files:read',
        name: '查看文件',
        description: '允许查看文件列表和文件详情',
        group: PermissionGroup.TASK,
      },
      {
        code: 'files:update',
        name: '更新文件',
        description: '允许更新文件信息',
        group: PermissionGroup.TASK,
      },
      {
        code: 'files:delete',
        name: '删除文件',
        description: '允许删除文件',
        group: PermissionGroup.TASK,
      },
    ];

    const permissionSeedData: PermissionInfo[] = [
      ...systemPermissions,
      ...workspacePermissions,
      ...projectPermissions,
      ...rolePermissions,
      ...taskPermissions,
      ...tagPermissions,
      ...notificationPermissions,
      ...filePermissions,
    ];
    
    // 保存所有权限数据到数据库
    for (const permissionData of permissionSeedData) {
      const existingPermission = await permissionRepository.findOneBy({
        code: permissionData.code,
      });

      if (!existingPermission) {
        const permission = permissionRepository.create(permissionData);
        await permissionRepository.save(permission);
        console.log(`创建权限: ${permission.name} (${permission.code})`);
      } else {
        console.log(`权限已存在: ${existingPermission.name} (${existingPermission.code})`);
      }
    }

    console.log('权限数据初始化完成');
  }
} 